<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JWT Token 测试页面</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        h1, h2 {
            color: #333;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"], input[type="password"], textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }
        textarea {
            height: 100px;
            resize: vertical;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .success {
            background-color: #28a745;
        }
        .success:hover {
            background-color: #1e7e34;
        }
        .warning {
            background-color: #ffc107;
            color: #212529;
        }
        .warning:hover {
            background-color: #e0a800;
        }
        .danger {
            background-color: #dc3545;
        }
        .danger:hover {
            background-color: #c82333;
        }
        .result {
            margin-top: 15px;
            padding: 15px;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            max-height: 300px;
            overflow-y: auto;
        }
        .result.success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .result.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .token-info {
            background-color: #e7f3ff;
            border: 1px solid #b3d9ff;
            padding: 15px;
            border-radius: 5px;
            margin-top: 15px;
        }
        .token-info h3 {
            margin-top: 0;
            color: #0066cc;
        }
        .token-display {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            padding: 10px;
            border-radius: 3px;
            font-family: 'Courier New', monospace;
            font-size: 11px;
            word-break: break-all;
            margin: 5px 0;
        }
        .flex-container {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }
        .flex-item {
            flex: 1;
            min-width: 300px;
        }
    </style>
    <!-- 引入jQuery本地文件 -->
    <script src="./js/jquery.min.js"></script>
</head>
<body>
    <h1>JWT Token 功能测试页面</h1>
    
    <!-- 1. 登录获取Token -->
    <div class="container">
        <h2>1. 登录获取Token</h2>
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" value="admin" placeholder="请输入用户名">
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" value="admin123" placeholder="请输入密码">
        </div>
        <button onclick="login()">登录</button>
        <div id="loginResult" class="result" style="display: none;"></div>
        
        <div id="tokenInfo" class="token-info" style="display: none;">
            <h3>Token 信息</h3>
            <div><strong>Access Token:</strong></div>
            <div id="accessTokenDisplay" class="token-display"></div>
            <div><strong>Refresh Token:</strong></div>
            <div id="refreshTokenDisplay" class="token-display"></div>
            <div><strong>用户信息:</strong></div>
            <div id="userInfoDisplay" class="token-display"></div>
        </div>
    </div>
    
    <!-- 2. 无Token测试 -->
    <div class="container">
        <h2>2. 无Token测试</h2>
        <div class="flex-container">
            <div class="flex-item">
                <h3>GET 请求测试</h3>
                <div class="form-group">
                    <label for="getName">姓名:</label>
                    <input type="text" id="getName" value="张三" placeholder="请输入姓名">
                </div>
                <button onclick="testGetWithoutToken()">测试 GET /test/hello</button>
                <div id="getResult" class="result" style="display: none;"></div>
            </div>
            
            <div class="flex-item">
                <h3>POST 请求测试</h3>
                <div class="form-group">
                    <label for="postName">姓名:</label>
                    <input type="text" id="postName" value="李四" placeholder="请输入姓名">
                </div>
                <button onclick="testPostWithoutToken()">测试 POST /test/hello</button>
                <div id="postResult" class="result" style="display: none;"></div>
            </div>
        </div>
    </div>
    
    <!-- 3. 带Token测试 -->
    <div class="container">
        <h2>3. 带Token测试</h2>
        
        <!-- Token输入框 -->
        <div class="form-group">
            <label for="manualToken">Token (手动输入或自动填充):</label>
            <textarea id="manualToken" placeholder="请输入Token或通过上方登录自动获取"></textarea>
        </div>
        
        <div class="flex-container">
            <div class="flex-item">
                <h3>方式1：自动使用登录Token</h3>
                <div class="form-group">
                    <label for="getNameWithToken">姓名:</label>
                    <input type="text" id="getNameWithToken" value="王五" placeholder="请输入姓名">
                </div>
                <button onclick="testGetWithToken()">测试 GET /test/hello (自动Token)</button>
                <button onclick="testPostWithToken()">测试 POST /test/hello (自动Token)</button>
                <div id="tokenTestResult" class="result" style="display: none;"></div>
            </div>
            
            <div class="flex-item">
                <h3>方式2：使用手动输入Token</h3>
                <div class="form-group">
                    <label for="getNameWithManualToken">姓名:</label>
                    <input type="text" id="getNameWithManualToken" value="赵六" placeholder="请输入姓名">
                </div>
                <button onclick="testGetWithManualToken()">测试 GET /test/hello (手动Token)</button>
                <button onclick="testPostWithManualToken()">测试 POST /test/hello (手动Token)</button>
                <div id="manualTokenTestResult" class="result" style="display: none;"></div>
            </div>
        </div>
    </div>
    
    <!-- 4. 易达电表数据接收接口测试 -->
    <div class="container">
        <h2>4. 易达电表数据接收接口测试</h2>
        <div class="form-group">
            <label for="deviceId">设备ID:</label>
            <input type="text" id="deviceId" value="METER001" placeholder="请输入设备ID">
        </div>
        <div class="form-group">
            <label for="furnaceNo">炉号:</label>
            <input type="text" id="furnaceNo" value="1" placeholder="请输入炉号">
        </div>
        <div class="form-group">
            <label for="voltage">电压(V):</label>
            <input type="text" id="voltage" value="220.5" placeholder="请输入电压值">
        </div>
        <div class="form-group">
            <label for="current">电流(A):</label>
            <input type="text" id="current" value="10.2" placeholder="请输入电流值">
        </div>
        <div class="form-group">
            <label for="power">功率(kW):</label>
            <input type="text" id="power" value="2.25" placeholder="请输入功率值">
        </div>
        <div class="form-group">
            <label for="energy">电能(kWh):</label>
            <input type="text" id="energy" value="100.5" placeholder="请输入电能值">
        </div>
        <div class="form-group">
            <label for="collectTime">采集时间:</label>
            <input type="text" id="collectTime" value="" placeholder="格式: 2024-01-15 10:30:00">
        </div>
        <button onclick="testMeterDataReceive()">测试易达电表数据接收接口</button>
        <div id="meterDataResult" class="result" style="display: none;"></div>
    </div>

    <!-- 4. Token管理 -->
    <div class="container">
        <h2>4. Token管理</h2>
        <button onclick="refreshToken()" class="success">刷新Token</button>
        <button onclick="getUserInfo()" class="warning">获取用户信息</button>
        <button onclick="logout()" class="danger">注销登录</button>
        <button onclick="clearTokens()" class="danger">清除Token</button>
        <div id="tokenManageResult" class="result" style="display: none;"></div>
    </div>
    
    <!-- 5. 易达电表数据接收接口测试 -->
    <div class="container">
        <h2>5. 易达电表数据接收接口测试</h2>
        
        <div class="flex-container">
            <div class="flex-item">
                <h3>单条数据接收测试</h3>
                <div class="form-group">
                    <label for="deviceId">设备ID:</label>
                    <input type="text" id="deviceId" value="METER001" placeholder="请输入设备ID">
                </div>
                <div class="form-group">
                    <label for="furnaceNo">炉号:</label>
                    <input type="text" id="furnaceNo" value="1" placeholder="请输入炉号">
                </div>
                <div class="form-group">
                    <label for="dcVoltage">直流电压(V):</label>
                    <input type="text" id="dcVoltage" value="220.5" placeholder="请输入直流电压值">
                </div>
                <div class="form-group">
                    <label for="dcCurrent">直流电流(A):</label>
                    <input type="text" id="dcCurrent" value="10.2" placeholder="请输入直流电流值">
                </div>
                <div class="form-group">
                    <label for="dcResistance">直流炉阻(Ω):</label>
                    <input type="text" id="dcResistance" value="0.022" placeholder="请输入直流炉阻值">
                </div>
                <div class="form-group">
                    <label for="activePower">有功功率(kW):</label>
                    <input type="text" id="activePower" value="2.25" placeholder="请输入有功功率值">
                </div>
                <div class="form-group">
                    <label for="furnaceEnergy">单炉电量(kWh):</label>
                    <input type="text" id="furnaceEnergy" value="100.5" placeholder="请输入单炉电量值">
                </div>
                <div class="form-group">
                    <label for="collectTime">采集时间:</label>
                    <input type="text" id="collectTime" value="" placeholder="格式: 2024-01-15 10:30:00">
                </div>
                <button onclick="testMeterDataReceive()">测试单条数据接收</button>
                <div id="meterDataResult" class="result" style="display: none;"></div>
            </div>
            

        </div>
        
        <div class="flex-container" style="margin-top: 20px;">
            <div class="flex-item">
                <h3>数据查询测试</h3>
                <div class="form-group">
                    <label for="queryDeviceId">设备ID:</label>
                    <input type="text" id="queryDeviceId" value="METER001" placeholder="请输入设备ID">
                </div>
                <button onclick="testGetLatestData()">获取最新数据</button>
                <div id="latestDataResult" class="result" style="display: none;"></div>
                
                <h4 style="margin-top: 20px;">按炉号查询数据</h4>
                <div class="form-group">
                    <label for="queryFurnaceNo">炉号:</label>
                    <input type="text" id="queryFurnaceNo" value="1" placeholder="请输入炉号">
                </div>
                <div class="form-group">
                    <label for="queryStartDate">开始日期:</label>
                    <input type="text" id="queryStartDate" placeholder="格式: 2024-01-15 00:00:00">
                </div>
                <div class="form-group">
                    <label for="queryEndDate">结束日期:</label>
                    <input type="text" id="queryEndDate" placeholder="格式: 2024-01-20 23:59:59">
                </div>
                <div class="form-group">
                    <label for="pageNum">页码:</label>
                    <input type="text" id="pageNum" value="1" placeholder="请输入页码">
                </div>
                <div class="form-group">
                    <label for="pageSize">每页记录数:</label>
                    <input type="text" id="pageSize" value="10" placeholder="请输入每页记录数">
                </div>
                <button onclick="testQueryList()">测试分页查询</button>
                <button id="ajaxTestBtn" class="success" style="margin-left: 10px;">AJAX测试分页查询</button>
                <div id="queryListResult" class="result" style="display: none;"></div>
                
                <!-- 数据表格显示 -->
                <div id="dataTable" style="margin-top: 20px; display: none;">
                    <h3>查询结果</h3>
                    <table style="width: 100%; border-collapse: collapse; margin-top: 10px;">
                        <thead>
                            <tr style="background-color: #f2f2f2;">
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">ID</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">设备ID</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">炉号</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">直流电压(V)</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">直流电流(A)</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">直流炉阻(Ω)</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">有功功率(kW)</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">单炉电量(kWh)</th>
                                <th style="border: 1px solid #ddd; padding: 8px; text-align: left;">采集时间</th>
                            </tr>
                        </thead>
                        <tbody id="dataTableBody">
                            <!-- 数据行将通过JavaScript动态添加 -->
                        </tbody>
                    </table>
                    
                    <!-- 分页控件 -->
                    <div id="pagination" style="margin-top: 15px; text-align: center;">
                        <button id="prevPage" onclick="changePage(-1)" style="margin-right: 10px;">上一页</button>
                        <span id="pageInfo">第 1 页 / 共 1 页</span>
                        <button id="nextPage" onclick="changePage(1)" style="margin-left: 10px;">下一页</button>
                    </div>
                </div>
            </div>
            
            <div class="flex-item">
                <h3>统计数据测试</h3>
                <div class="form-group">
                    <label for="statsType">统计类型:</label>
                    <select id="statsType">
                        <option value="day">按日统计</option>
                        <option value="week">按周统计</option>
                        <option value="month">按月统计</option>
                        <option value="year">按年统计</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="startDate">开始日期:</label>
                    <input type="date" id="startDate" value="2024-01-01">
                </div>
                <div class="form-group">
                    <label for="endDate">结束日期:</label>
                    <input type="date" id="endDate" value="2024-12-31">
                </div>
                <button onclick="testGetStatistics()">获取统计数据</button>
                <div id="statisticsResult" class="result" style="display: none;"></div>
            </div>
        </div>
    </div>
    
    <script>
        let currentAccessToken = '';
        let currentRefreshToken = '';
        
        // 登录获取Token
        function login() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            if (!username || !password) {
                showResult('loginResult', '请填写完整的登录信息', 'error');
                return;
            }
            
            const loginData = {
                username: username,
                password: password
            };
            
            fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(loginData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    currentAccessToken = data.data.tokenInfo.accessToken;
                    currentRefreshToken = data.data.tokenInfo.refreshToken;
                    
                    // 自动填充到手动输入框
                    document.getElementById('manualToken').value = currentAccessToken;
                    
                    showResult('loginResult', '登录成功!\n' + JSON.stringify(data, null, 2), 'success');
                    
                    // 显示Token信息
                    document.getElementById('accessTokenDisplay').textContent = currentAccessToken;
                    document.getElementById('refreshTokenDisplay').textContent = currentRefreshToken;
                    document.getElementById('userInfoDisplay').textContent = JSON.stringify(data.data.userInfo, null, 2);
                    document.getElementById('tokenInfo').style.display = 'block';
                } else {
                    showResult('loginResult', '登录失败: ' + data.msg, 'error');
                }
            })
            .catch(error => {
                showResult('loginResult', '登录错误: ' + error.message, 'error');
            });
        }
        
        // 无Token GET测试
        function testGetWithoutToken() {
            const name = document.getElementById('getName').value;
            if (!name) {
                showResult('getResult', '请输入姓名', 'error');
                return;
            }
            
            fetch(`/test/hello?name=${encodeURIComponent(name)}`, {
                method: 'GET'
            })
            .then(response => response.json())
            .then(data => {
                showResult('getResult', 'GET请求成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('getResult', 'GET请求错误: ' + error.message, 'error');
            });
        }
        
        // 无Token POST测试
        function testPostWithoutToken() {
            const name = document.getElementById('postName').value;
            if (!name) {
                showResult('postResult', '请输入姓名', 'error');
                return;
            }
            
            const formData = new FormData();
            formData.append('name', name);
            
            fetch('/test/hello', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                showResult('postResult', 'POST请求成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('postResult', 'POST请求错误: ' + error.message, 'error');
            });
        }
        
        // 带Token GET测试（自动Token）
        function testGetWithToken() {
            if (!currentAccessToken) {
                showResult('tokenTestResult', '请先登录获取Token', 'error');
                return;
            }
            
            const name = document.getElementById('getNameWithToken').value;
            if (!name) {
                showResult('tokenTestResult', '请输入姓名', 'error');
                return;
            }
            
            fetch(`/test/hello?name=${encodeURIComponent(name)}`, {
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + currentAccessToken
                }
            })
            .then(response => response.json())
            .then(data => {
                showResult('tokenTestResult', 'GET请求(带Token)成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('tokenTestResult', 'GET请求(带Token)错误: ' + error.message, 'error');
            });
        }
        
        // 带Token POST测试（自动Token）
        function testPostWithToken() {
            if (!currentAccessToken) {
                showResult('tokenTestResult', '请先登录获取Token', 'error');
                return;
            }
            
            const name = document.getElementById('getNameWithToken').value;
            if (!name) {
                showResult('tokenTestResult', '请输入姓名', 'error');
                return;
            }
            
            const formData = new FormData();
            formData.append('name', name);
            
            fetch('/test/hello', {
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + currentAccessToken
                },
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                showResult('tokenTestResult', 'POST请求(带Token)成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('tokenTestResult', 'POST请求(带Token)错误: ' + error.message, 'error');
            });
        }
        
        // 带Token GET测试（手动Token）
        function testGetWithManualToken() {
            const manualToken = document.getElementById('manualToken').value.trim();
            if (!manualToken) {
                showResult('manualTokenTestResult', '请输入Token', 'error');
                return;
            }
            
            const name = document.getElementById('getNameWithManualToken').value;
            if (!name) {
                showResult('manualTokenTestResult', '请输入姓名', 'error');
                return;
            }
            
            fetch(`/test/hello?name=${encodeURIComponent(name)}`, {
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + manualToken
                }
            })
            .then(response => response.json())
            .then(data => {
                showResult('manualTokenTestResult', 'GET请求(手动Token)成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('manualTokenTestResult', 'GET请求(手动Token)错误: ' + error.message, 'error');
            });
        }
        
        // 带Token POST测试（手动Token）
        function testPostWithManualToken() {
            const manualToken = document.getElementById('manualToken').value.trim();
            if (!manualToken) {
                showResult('manualTokenTestResult', '请输入Token', 'error');
                return;
            }
            
            const name = document.getElementById('getNameWithManualToken').value;
            if (!name) {
                showResult('manualTokenTestResult', '请输入姓名', 'error');
                return;
            }
            
            const formData = new FormData();
            formData.append('name', name);
            
            fetch('/test/hello', {
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + manualToken
                },
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                showResult('manualTokenTestResult', 'POST请求(手动Token)成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('manualTokenTestResult', 'POST请求(手动Token)错误: ' + error.message, 'error');
            });
        }
        
        // 刷新Token
        function refreshToken() {
            if (!currentRefreshToken) {
                showResult('tokenManageResult', '没有可用的Refresh Token，请先登录', 'error');
                return;
            }
            
            fetch('/api/auth/refresh', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + currentRefreshToken
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    currentAccessToken = data.data.accessToken;
                    // 更新手动输入框中的token
                    document.getElementById('manualToken').value = currentAccessToken;
                    // 更新显示
                    document.getElementById('accessTokenDisplay').textContent = currentAccessToken;
                    showResult('tokenManageResult', 'Token刷新成功:\n' + JSON.stringify(data, null, 2), 'success');
                } else {
                    showResult('tokenManageResult', 'Token刷新失败: ' + data.msg, 'error');
                }
            })
            .catch(error => {
                showResult('tokenManageResult', 'Token刷新错误: ' + error.message, 'error');
            });
        }
        
        // 获取用户信息
        function getUserInfo() {
            if (!currentAccessToken) {
                showResult('tokenManageResult', '请先登录获取Token', 'error');
                return;
            }
            
            fetch('/api/auth/userinfo', {
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer ' + currentAccessToken
                }
            })
            .then(response => response.json())
            .then(data => {
                showResult('tokenManageResult', '用户信息获取成功:\n' + JSON.stringify(data, null, 2), 'success');
            })
            .catch(error => {
                showResult('tokenManageResult', '用户信息获取错误: ' + error.message, 'error');
            });
        }
        
        // 注销登录
        function logout() {
            if (!currentAccessToken) {
                showResult('tokenManageResult', '当前未登录', 'error');
                return;
            }
            
            fetch('/api/auth/logout', {
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + currentAccessToken
                }
            })
            .then(response => response.json())
            .then(data => {
                showResult('tokenManageResult', '注销成功:\n' + JSON.stringify(data, null, 2), 'success');
                clearTokens();
            })
            .catch(error => {
                showResult('tokenManageResult', '注销错误: ' + error.message, 'error');
            });
        }
        
        // 清除Token
        function clearTokens() {
            currentAccessToken = '';
            currentRefreshToken = '';
            document.getElementById('manualToken').value = '';
            document.getElementById('accessTokenDisplay').textContent = '';
            document.getElementById('refreshTokenDisplay').textContent = '';
            document.getElementById('userInfoDisplay').textContent = '';
            document.getElementById('tokenInfo').style.display = 'none';
            showResult('tokenManageResult', 'Token已清除', 'success');
        }
        
        // 易达电表数据接收接口测试函数
         
         // 测试单条数据接收
         function testMeterDataReceive() {
             if (!currentAccessToken) {
                 showResult('meterDataResult', '请先登录获取Token', 'error');
                 return;
             }
             
             const meterData = {
                 deviceId: document.getElementById('deviceId').value,
                 furnaceNo: parseInt(document.getElementById('furnaceNo').value),
                 voltage: parseFloat(document.getElementById('voltage').value),
                 current: parseFloat(document.getElementById('current').value),
                 power: parseFloat(document.getElementById('power').value),
                 energy: parseFloat(document.getElementById('energy').value)
             };
             
             fetch('/api/ydMeterData/receive', {
                 method: 'POST',
                 headers: {
                     'Content-Type': 'application/json',
                     'Authorization': 'Bearer ' + currentAccessToken
                 },
                 body: JSON.stringify(meterData)
             })
             .then(response => response.json())
             .then(data => {
                 if (data.code === 0) {
                     showResult('meterDataResult', '单条数据接收成功:\n' + JSON.stringify(data, null, 2), 'success');
                 } else {
                     showResult('meterDataResult', '单条数据接收失败: ' + data.msg, 'error');
                 }
             })
             .catch(error => {
                 showResult('meterDataResult', '单条数据接收错误: ' + error.message, 'error');
             });
         }
         
         // 显示结果
        function showResult(elementId, message, type) {
            const element = document.getElementById(elementId);
            element.textContent = message;
            element.className = 'result ' + type;
            element.style.display = 'block';
        }
        
        // 易达电表数据接收接口测试
        function testMeterDataReceive() {
            const deviceId = document.getElementById('deviceId').value;
            const furnaceNo = document.getElementById('furnaceNo').value;
            const dcVoltage = document.getElementById('dcVoltage').value;
            const dcCurrent = document.getElementById('dcCurrent').value;
            const dcResistance = document.getElementById('dcResistance').value;
            const activePower = document.getElementById('activePower').value;
            const furnaceEnergy = document.getElementById('furnaceEnergy').value;
            let collectTime = document.getElementById('collectTime').value;
            
            // 如果没有输入采集时间，使用当前时间
            if (!collectTime) {
                const now = new Date();
                collectTime = now.getFullYear() + '-' + 
                    String(now.getMonth() + 1).padStart(2, '0') + '-' + 
                    String(now.getDate()).padStart(2, '0') + ' ' + 
                    String(now.getHours()).padStart(2, '0') + ':' + 
                    String(now.getMinutes()).padStart(2, '0') + ':' + 
                    String(now.getSeconds()).padStart(2, '0');
                document.getElementById('collectTime').value = collectTime;
            }
            
            if (!deviceId || !furnaceNo || !dcVoltage || !dcCurrent || !dcResistance || !activePower || !furnaceEnergy) {
                showResult('meterDataResult', '请填写完整的电表数据信息', 'error');
                return;
            }
            
            const meterData = {
                deviceId: deviceId,
                furnaceNo: parseInt(furnaceNo),
                dcVoltage: parseFloat(dcVoltage),
                dcCurrent: parseFloat(dcCurrent),
                dcResistance: parseFloat(dcResistance),
                activePower: parseFloat(activePower),
                furnaceEnergy: parseFloat(furnaceEnergy),
                collectTime: collectTime
            };
            
            fetch('/api/ydMeterData/receive', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': currentAccessToken ? 'Bearer ' + currentAccessToken : ''
                },
                body: JSON.stringify(meterData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 0) {
                    showResult('meterDataResult', '电表数据接收成功:\n' + JSON.stringify(data, null, 2), 'success');
                } else {
                    showResult('meterDataResult', '电表数据接收失败: ' + data.msg, 'error');
                }
            })
            .catch(error => {
                showResult('meterDataResult', '电表数据接收错误: ' + error.message, 'error');
            });
        }
        

         
         // 获取最新数据测试
         function testGetLatestData() {
             const deviceId = document.getElementById('queryDeviceId').value;
             
             if (!deviceId) {
                 showResult('latestDataResult', '请输入设备ID', 'error');
                 return;
             }
             
             fetch('/api/ydMeterData/latest/' + deviceId, {
                 method: 'GET',
                 headers: {
                     'Authorization': currentAccessToken ? 'Bearer ' + currentAccessToken : ''
                 }
             })
             .then(response => response.json())
             .then(data => {
                 if (data.code === 0) {
                     showResult('latestDataResult', '最新数据获取成功:\n' + JSON.stringify(data, null, 2), 'success');
                 } else {
                     showResult('latestDataResult', '最新数据获取失败: ' + data.msg, 'error');
                 }
             })
             .catch(error => {
                 showResult('latestDataResult', '最新数据获取错误: ' + error.message, 'error');
             });
         }
         
         // 获取统计数据测试
         function testGetStatistics() {
             const statsType = document.getElementById('statsType').value;
             const startDate = document.getElementById('startDate').value;
             const endDate = document.getElementById('endDate').value;
             
             if (!startDate || !endDate) {
                 showResult('statisticsResult', '请选择开始和结束日期', 'error');
                 return;
             }
             
             const params = new URLSearchParams({
                 type: statsType,
                 startDate: startDate,
                 endDate: endDate
             });
             
             fetch('/api/ydMeterData/statistics?' + params, {
                 method: 'GET',
                 headers: {
                     'Authorization': currentAccessToken ? 'Bearer ' + currentAccessToken : ''
                 }
             })
             .then(response => response.json())
             .then(data => {
                 if (data.code === 0) {
                     showResult('statisticsResult', '统计数据获取成功:\n' + JSON.stringify(data, null, 2), 'success');
                 } else {
                     showResult('statisticsResult', '统计数据获取失败: ' + data.msg, 'error');
                 }
             })
             .catch(error => {
                 showResult('statisticsResult', '统计数据获取错误: ' + error.message, 'error');
             });
         }
         
         // 按炉号查询数据列表测试
         // 全局变量，用于存储分页信息
         let currentPage = 1;
         let totalPages = 1;
         let totalRecords = 0;
         
         // 测试分页查询
         function testQueryList() {
             const furnaceNo = document.getElementById('queryFurnaceNo').value;
             const startDate = document.getElementById('queryStartDate').value;
             const endDate = document.getElementById('queryEndDate').value;
             const pageNum = document.getElementById('pageNum').value || 1;
             const pageSize = document.getElementById('pageSize').value || 10;
             
             // 验证至少有一个查询条件
             if (!furnaceNo && !startDate && !endDate) {
                 showResult('queryListResult', '请至少输入一个查询条件（炉号、开始日期或结束日期）', 'error');
                 return;
             }
             
             // 构建查询参数
             let queryParams = new URLSearchParams();
             if (furnaceNo) queryParams.append('furnaceNo', furnaceNo);
             if (startDate) queryParams.append('startDate', startDate);
             if (endDate) queryParams.append('endDate', endDate);
             queryParams.append('pageNum', pageNum);
             queryParams.append('pageSize', pageSize);
             
             // 发送请求
             fetch(`/api/ydMeterData/queryList?${queryParams.toString()}`, {
                 method: 'GET',
                 headers: {
                     'Authorization': currentAccessToken ? 'Bearer ' + currentAccessToken : ''
                 }
             })
             .then(response => response.json())
             .then(data => {
                 if (data.code === 0) {
                     // 更新全局变量
                     currentPage = parseInt(pageNum);
                     totalRecords = data.total;
                     totalPages = Math.ceil(totalRecords / pageSize);
                     
                     // 显示结果
                     showResult('queryListResult', `查询成功，共找到 ${data.total} 条记录:\n` + JSON.stringify(data, null, 2), 'success');
                     
                     // 渲染数据表格
                     renderDataTable(data.rows);
                     
                     // 更新分页信息
                     updatePagination();
                     
                     // 显示数据表格
                     document.getElementById('dataTable').style.display = 'block';
                 } else {
                     showResult('queryListResult', '查询失败: ' + data.msg, 'error');
                     document.getElementById('dataTable').style.display = 'none';
                 }
             })
             .catch(error => {
                 showResult('queryListResult', '查询错误: ' + error.message, 'error');
                 document.getElementById('dataTable').style.display = 'none';
             });
         }
         
         // 渲染数据表格
         function renderDataTable(rows) {
             const tableBody = document.getElementById('dataTableBody');
             tableBody.innerHTML = '';
             
             if (!rows || rows.length === 0) {
                 const row = document.createElement('tr');
                 row.innerHTML = '<td colspan="9" style="text-align: center; padding: 10px;">没有找到数据</td>';
                 tableBody.appendChild(row);
                 return;
             }
             
             rows.forEach(item => {
                 const row = document.createElement('tr');
                 row.innerHTML = `
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.id || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.deviceId || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.furnaceNo || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.dcVoltage || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.dcCurrent || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.dcResistance || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.activePower || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.furnaceEnergy || ''}</td>
                     <td style="border: 1px solid #ddd; padding: 8px;">${item.collectTime || ''}</td>
                 `;
                 tableBody.appendChild(row);
             });
         }
         
         // 更新分页信息
         function updatePagination() {
             document.getElementById('pageInfo').textContent = `第 ${currentPage} 页 / 共 ${totalPages} 页 (共${totalRecords}条记录)`;
             document.getElementById('prevPage').disabled = currentPage <= 1;
             document.getElementById('nextPage').disabled = currentPage >= totalPages;
         }
         
         // 切换页码
         function changePage(delta) {
             const newPage = currentPage + delta;
             if (newPage < 1 || newPage > totalPages) return;
             
             document.getElementById('pageNum').value = newPage;
             testQueryList();
         }
         
         // AJAX方式测试分页查询
         $(document).ready(function() { 
             $('#ajaxTestBtn').click(function() { 
                 const furnaceNo = $('#queryFurnaceNo').val();
                 const startDate = $('#queryStartDate').val();
                 const endDate = $('#queryEndDate').val();
                 const pageNum = $('#pageNum').val() || 1;
                 const pageSize = $('#pageSize').val() || 10;
                 
                 // 验证至少有一个查询条件
                 if (!furnaceNo && !startDate && !endDate) {
                     $('#queryListResult').html('请至少输入一个查询条件（炉号、开始日期或结束日期）').show();
                     return;
                 }
                 
                 // 构建查询参数
                 let queryParams = {};
                 if (furnaceNo) queryParams.furnaceNo = furnaceNo;
                 if (startDate) queryParams.startDate = startDate;
                 if (endDate) queryParams.endDate = endDate;
                 queryParams.pageNum = pageNum;
                 queryParams.pageSize = pageSize;
                 debugger
                 $.ajax({ 
                     url: '/api/ydMeterData/queryList',  // 接口地址 
                     type: 'GET',           // 请求类型 
                     data: queryParams,      // 请求参数
                     dataType: 'json',      // 预期返回的数据类型
                     headers: {
                         'Authorization': currentAccessToken ? 'Bearer ' + currentAccessToken : ''
                     },
                     success: function(data) { 
                         // 请求成功回调 
                         if (data.code === 0) {
                             // 更新全局变量
                             currentPage = parseInt(pageNum);
                             totalRecords = data.total;
                             totalPages = Math.ceil(totalRecords / pageSize);
                             
                             // 显示结果
                             $('#queryListResult').html(`查询成功，共找到 ${data.total} 条记录:<br>` + 
                                 JSON.stringify(data, null, 2)).show();
                             
                             // 渲染数据表格
                             renderDataTable(data.rows);
                             
                             // 更新分页信息
                             updatePagination();
                         } else {
                             $('#queryListResult').html('查询失败: ' + data.msg).show();
                         }
                     }, 
                     error: function(xhr, status, error) { 
                         // 请求失败回调 
                         $('#queryListResult').html('请求失败: ' + xhr.status).show(); 
                     } 
                 }); 
             });
         });
        </script></body></html>